package org.nicocube.airain.domain.client.character;

import java.io.Serializable;
import java.util.List;

import org.nicocube.airain.domain.client.data.OrderedStorable;
import org.nicocube.airain.domain.client.gamedate.GameDate;

import com.db4o.config.annotations.Indexed;

/**
 * This class represents what a {@link GameCharacter} can do on a given date.    
 * 
 * @author nico
 * @version 0.1
 */
public class Action extends OrderedStorable implements Serializable {

	// as for version 0.1
	private static final long serialVersionUID = 1L;
	
	private ActionType type;
	@Indexed
	private Long gameCharacterOrder;
	private GameDate gameDate;
	
	/**
	 * Static Factory method for building {@link Action} 
	 * 
	 * @param type what kind of action 
	 * @param gameCharacterOrder the unique order of the {@link GameCharacter}
	 * @param gameDate the begin date of the action
	 * @return a brand new {@link Action}
	 */
	public static Action build(ActionType type,Long gameCharacterOrder,GameDate gameDate)  {
		Action res = new Action();
		res.type = type;
		res.gameCharacterOrder = gameCharacterOrder;
		res.gameDate = gameDate;
		return res;
	}
	
	/**
	 * the constructor secured by protected access modifier
	 */
	protected Action() {}
	
	/**
	 * Retrieve the {@link ActionType} of the {@link Action} 
	 * @return the type
	 */
	public ActionType getType() {
		return type;
	}

	/**
	 * Retrieve the unique long order of the {@link GameCharacter}
	 * @return
	 */
	public Long getGameCharacter() {
		return gameCharacterOrder;
	}

	/**
	 * Retrieve the {@link GameDate} of begining of the {@link Action}
	 * @return
	 */
	public GameDate getGameDate() {
		return gameDate;
	}
	
	/**
	 * Modify the {@link GameDate} of begining of the {@link Action} 
	 * @param gameDate
	 */
	public void changeGameDate(GameDate gameDate) {
		this.gameDate = gameDate;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		return sb
			.append("\n{")
			.append("o:").append(getOrder())
			.append(",")
			.append("gc:").append(getGameCharacter())
			.append(",")
			.append("t:").append(getType())
			.append(",")
			.append("d:").append(getGameDate())
			.append("}")
			.toString();
	}
	
	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Action)) return false;
		Action a = (Action) obj;
		return (isSaved() && a.isSaved())
			&& (getOrder().equals(a.getOrder()))
			&& (getType().equals(a.getType()))
			&& (getGameCharacter().equals(a.getGameCharacter()))
			&& (getGameDate().equals(a.getGameDate()));
	}

	//@Override
	public List<Object> dependencies() {
		return null;
	}
	
}
